package com.congee02;

public class FindMaxAverage {

    public double findMaxAverage(int[] nums, int k) {

        int n = nums.length;

        long sum = 0;
        for (int i = 0 ; i < k ; i ++ ) {
            sum += nums[i];
        }
        long maxSum = sum;

        for (int r = k ; r < n ; r ++ ) {
            int l = r - k + 1;
            sum += - nums[l - 1] + nums[r];
            maxSum = Math.max(maxSum, sum);
        }

        return maxSum / (1. * k);
    }

    public static void main(String[] args) {
        FindMaxAverage s = new FindMaxAverage();
        System.out.println(s.findMaxAverage(new int[]{1, 12, -5, -6, 50, 3}, 4));
    }

}
